//
//  H5UserInfoBridge.m
//  UniLoginSDK
//
//  Created by xiaofeng on 13-9-9.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "MULH5UserInfoBridge.h"
#import "MULLoginManager.h"
#import "MULXAuthUtils.h"
#import "MULXAuthHttpRequest.h"

@interface MULH5UserInfoBridge()
@property(nonatomic, strong)UIWebView *webview;

+ (MULH5UserInfoBridge*)sharedInstance;
@end

@implementation MULH5UserInfoBridge

+ (void)updateH5UserCookie;
{
// 已经不需要通过webview来设置cookie了，通过接口response header中的Set-Cookie来设置相应的cookie
//    if ([[MULLoginManager sharedInstance] hasLogin])
//    {
//        [self setH5UserCookie:nil];
//    }
//    else
//    {
//        [self cleanH5UserCookie:nil];
//    }
}


+ (void)logPrintAllCookie
{
    NSHTTPCookieStorage *cookieSotrage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *allCookies = [cookieSotrage cookies];
    NSLog(@"allCookies>> \n %@", allCookies);
}

+ (void)cleanAllCookies;
{
    [[MULH5UserInfoBridge sharedInstance] nativeCleanAllCookies];
}

+ (MULH5UserInfoBridge*)sharedInstance;
{
    static MULH5UserInfoBridge *_sharedInstance = nil;
	if (_sharedInstance == nil)
	{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedInstance = [[MULH5UserInfoBridge alloc] init];
			
        });
	}
	
	return _sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

- (void)nativeCleanAllCookies
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cookie"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *allCookies = [cookieStorage cookies];
    
    NSMutableArray *toDeleteArray = [NSMutableArray array];
    NSSet *keys = [NSSet setWithObjects:@"userinfo", nil];
    NSSet *domains = [NSSet setWithObjects:@".youyuwei.com", nil];
    for (NSHTTPCookie *cookie in allCookies)
    {
        NSString *domain = cookie.domain;
        NSString *key = cookie.name;
        
        if ([keys containsObject:key] && [domains containsObject:domain])
        {
            [toDeleteArray addObject:cookie];
        }
    }
    
    [toDeleteArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSHTTPCookie *cookie = obj;
        NSLog(@"delete cookie: key=>%@, domain=>%@", cookie.name, cookie.domain);
        [cookieStorage deleteCookie:cookie];
    }];
}



@end
